from neo4j import GraphDatabase
import pandas as pd

data = pd.read_csv('disease.csv')
data = data.fillna('无')
df = list(data.itertuples(index=False,name = None))

URI = "bolt://localhost:7687"
AUTH = ("admin", "73@TuGraph")
with GraphDatabase.driver(URI, auth=AUTH) as client:
    session = client.session(database="default")
    # 读入disease
    # for name in data['name']:
    #     querry = "create (n1:disease {name:'"+name+"'})"
    #     session.run(querry)
    # for name in data['alias']:
    #     print(name)
    #     querry = "create (n1:alias {name:'" + name + "'})"
    #     session.run(querry)
    for d in df:
        disease = d[0]
        alias = d[1].split()
        part = d[2].split()
        age = d[3].split()
        infection = d[4]
        insurance = d[5]
        department = d[6].split()
        check_list = d[7].split()
        symptom = d[8].split()
        complication = d[9].split()
        treatment = d[10].split()
        drug = d[11].split()
        period = d[12]
        rate = d[13]
        money = d[14]

        disease_Node = "merge (n1:disease {name:'"+disease+"'})"
        alias_Nodes = ["merge (n2:alias {name:'"+i+"'})"for i in alias]
        part_Nodes = ["merge (n2:part {name:'"+i+"'})"for i in part]
        age_Nodes = ["merge (n2:age {name:'"+i+"'})"for i in age]
        infection_Node = "merge (n2:infection {name:'"+infection+"'})"
        insurance_Node = "merge (n2:insurance {name:'"+insurance+"'})"
        department_Nodes = ["merge (n2:department {name:'"+i+"'})"for i in department]
        check_list_Nodes = ["merge (n2:checklist {name:'"+i+"'})"for i in check_list]
        symptom_Nodes = ["merge (n2:symptom {name:'"+i+"'})"for i in symptom]
        complication_Nodes = ["merge (n2:complication {name:'"+i+"'})"for i in complication]
        treatment_Nodes = ["merge (n2:treatment {name:'"+i+"'})" for i in treatment]
        drug_Nodes = ["merge (n2:drug {name:'"+i+"'})"for i in drug]
        period_Node = "merge (n2:period {name:'"+period+"'})"
        rate_Node = "merge (n2:rate {name:'"+rate+"'})"
        money_Node = "merge (n2:money {name:'"+money+"'})"

        # 将节点加入到图数据库中
        session.run(disease_Node)
        for node in alias_Nodes:
            session.run(node)
        for node in part_Nodes:
            session.run(node)
        for node in age_Nodes:
            session.run(node)
        session.run(infection_Node)
        session.run(insurance_Node)
        for node in department_Nodes:
            session.run(node)
        for node in check_list_Nodes:
            session.run(node)
        for node in symptom_Nodes:
            session.run(node)
        for node in complication_Nodes:
            session.run(node)
        for node in treatment_Nodes:
            session.run(node)
        for node in drug_Nodes:
            session.run(node)
        session.run(period_Node)
        session.run(rate_Node)
        session.run(money_Node)

        for node in alias_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_alias]->(n2)"
            session.run(querry)

        for node in part_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_part]->(n2)"
            session.run(querry)
        for node in age_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_age]->(n2)"
            session.run(querry)
        querry = "match " + disease_Node[6:] + "," + infection_Node[6:] + "merge (n1)-[r:is_infection]->(n2)"
        session.run(querry)
        querry = "match " + disease_Node[6:] + "," + insurance_Node[6:] + "merge (n1)-[r:is_insurance]->(n2)"
        session.run(querry)
        for node in department_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_department]->(n2)"
            session.run(querry)
        for node in check_list_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_checklist]->(n2)"
            session.run(querry)
        for node in symptom_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_symptom]->(n2)"
            session.run(querry)
        for node in complication_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_complication]->(n2)"
            session.run(querry)
        for node in treatment_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_treatment]->(n2)"
            session.run(querry)
        for node in drug_Nodes:
            querry = "match " + disease_Node[6:] + "," + node[6:] + "merge (n1)-[r:is_drug]->(n2)"
            session.run(querry)
        querry = "match " + disease_Node[6:] + "," + period_Node[6:] + "merge (n1)-[r:is_period]->(n2)"
        session.run(querry)
        querry = "match " + disease_Node[6:] + "," + rate_Node[6:] + "merge (n1)-[r:is_rate]->(n2)"
        session.run(querry)
        querry = "match " + disease_Node[6:] + "," + money_Node[6:] + "merge (n1)-[r:is_money]->(n2)"
        session.run(querry)

print('数据传输完成')